package util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * ClassName:DaoUtil
 * Package:Util
 *
 * @date:2021/10/26 6:39 下午
 * @author: Tiangle
 */
public class DaoUtil {
    private static DaoUtil du = null;
    private static SqlSessionFactory sqf = null;
    private DaoUtil(){

    }
    public static DaoUtil getDu(){
        if (du==null){
            du = new DaoUtil();
            String resource = "mybatis-config.xml";
            InputStream inputStream = null;
            try {
                inputStream = Resources.getResourceAsStream(resource);
            } catch (IOException e) {
                e.printStackTrace();
            }
            sqf = new SqlSessionFactoryBuilder().build(inputStream);
            //通过SqlSessionFacotry得到SqlSession
        }
        return du;
    }


    public SqlSession getSqlS(){
        return sqf.openSession();

    }

}
